package edu.uwm.capstone.diasmart.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import edu.uwm.capstone.diasmart.AddEditEntryActivity;
import edu.uwm.capstone.diasmart.R;
import edu.uwm.capstone.diasmart.database.Category;
import edu.uwm.capstone.diasmart.database.Reminder;
import edu.uwm.capstone.util.DebugUtil;
import edu.uwm.capstone.util.Identifiable;

public class AddEntryHelper {

	public static void updateCategorySelection(Spinner paramSpinner,
			long paramLong) {
		for (int i = 0; i < paramSpinner.getCount(); i++) {
			if (((Category) paramSpinner.getItemAtPosition(i)).getId() == paramLong) {
				paramSpinner.setSelection(i);
			}
		}
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void updateSpinnerAdapter(Spinner paramSpinner, int paramInt,
			Object[] categories) {
		Log.d("Diasmart", "Spinner = " + paramSpinner + " paramInt ="
				+ paramInt + " Object[] =" + categories);
		long id = -1L;
		Identifiable localIdentifiable = (Identifiable) paramSpinner
				.getSelectedItem();
		if (localIdentifiable != null)
			id = localIdentifiable.getId();
		ArrayAdapter categoryTypeAdapter = new ArrayAdapter(
				paramSpinner.getContext(), R.layout.ellipsize_spinner_item,
				categories);
		categoryTypeAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		paramSpinner.setPromptId(paramInt);
		paramSpinner.setAdapter(categoryTypeAdapter);
		categoryTypeAdapter.notifyDataSetChanged();
		for (int i = 0; i < categoryTypeAdapter.getCount(); i++) {
			if (((Identifiable) categoryTypeAdapter.getItem(i)).getId() == id) {
				DebugUtil.debug(AddEntryHelper.class,
						"Updating spinner to selection " + i + " for "
								+ paramSpinner.getItemAtPosition(i));
				paramSpinner.setSelection(i);

			}
		}
	}

	public static String getAddPreferenceType(Context activityContext) {
		String str = activityContext
				.getString(R.string.behavior_add_button_preference_key);
		return PreferenceManager.getDefaultSharedPreferences(activityContext)
				.getString(str, "add");
	}

	public static void showAddDialog(Activity activity) {
		Intent globalVariables = new Intent(activity,
				AddEditEntryActivity.class);
		globalVariables.putExtra("edu.uwm.capstone.diasmart.EntryInfo.type", 0);
		globalVariables.putExtra("edu.uwm.capstone.diasmart.EntryInfo.mode", 0);
		activity.startActivityForResult(globalVariables, 1);
	}

	public static void updateDefaultReminderForCategory(Spinner spinner,
			int[] arrayOfInt, long id) {
		
		Log.d("Diasmart", "Inside UpdateDefaultReminderForCategory Spinner = "+spinner+" Integer = "+arrayOfInt+" id = "+id);
		SpinnerAdapter spinnerAdapter = spinner.getAdapter();
		Log.d("Diasmart",""+spinnerAdapter);
		Reminder reminder;
		try{
			Log.d("Diasmart","Spinner count = "+ spinnerAdapter.getCount());
			
		}
		catch(Exception e){
			Log.e("Diasmart","SpinnerAdapter get count error"+ e.getStackTrace());
		}
//		for (int i = 0; i <=spinnerAdapter.getCount(); i++) {
//			do {
//				reminder = (Reminder) spinnerAdapter.getItem(i);
//			} while (!reminder.isDefaultCategory(id));
//			for (int j = 0; j < arrayOfInt.length; j++) {
//				if (reminder.isDefaultType(arrayOfInt[j]))
//					spinner.setSelection(j);
//			}
//		}
	}
}
